/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GraphBox.java
 *
 * Created on 4 janv. 2009, 23:26:57
 */

package itrafgen.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Hour;

/**
 *
 * @author sebastienhoerner
 */
public class GraphBox extends javax.swing.JPanel {

    public TimeSeries series_ligne_1;  //  @jve:decl-index=0:
	public NumberAxis rangeAxis;
	public XYPlot plot;  //  @jve:decl-index=0:
	public JFreeChart chart;
    int num = 0;
    /** Creates new form GraphBox */
    public GraphBox() {
        initComponents();
        ChartPanel chartPanel;






		Color COULEURLIGNE1 = Color.GREEN;

		Color COULEURLIGNE2 = Color.red;



	// On ajoute les lignes dans le dataset

		TimeSeriesCollection dataSet = new TimeSeriesCollection();



	// Creation des lignes

		series_ligne_1 = new TimeSeries("Latence réseau", FixedMillisecond.class);

		//TimeSeries series_ligne_2 = new TimeSeries("Ligne 2", Hour.class);



	// Ajout des ligne dans le dataset

		dataSet.addSeries(series_ligne_1);

		//dataSet.addSeries(series_ligne_2);



	// Creation du graphique

		chart = ChartFactory.createTimeSeriesChart("","X","Y",dataSet,true,false,false);



		chartPanel = new ChartPanel(chart, true);



		chartPanel.setBounds(new Rectangle(5, 5, 495, 215));
		this.add(chartPanel, null);
	// Les dimmensions du graphique

		Dimension d = new Dimension(495, 215);



		chartPanel.setMaximumSize(d);

		chartPanel.setPreferredSize(d);

		chartPanel.setMinimumSize(d);



		 plot = (XYPlot) chart.getPlot();



	// On definie une couleur pour les lignes

		plot.getRenderer().setSeriesPaint(0,COULEURLIGNE1);

		plot.getRenderer().setSeriesPaint(1,COULEURLIGNE2);



	// On definie une couleur de fond pour le graphique

		plot.setBackgroundPaint(Color.white);



		rangeAxis = (NumberAxis) plot.getRangeAxis();




	// On fixe une taille pour l'axe des ordonnÈes

		rangeAxis.setUpperBound(50.0);







		series_ligne_1.setNotify(false);

		//new org.jfree.data.time.Millisecond(0);

		 java.util.Date today = new java.util.Date();

	/*	series_ligne_1.add(new FixedMillisecond(today.getTime()), 5.55);

		series_ligne_1.add(new FixedMillisecond(today.getTime() + 100), 2.55);

		series_ligne_1.add(new FixedMillisecond(today.getTime() + 200), 3);

		series_ligne_1.add(new FixedMillisecond(today.getTime() + 500), 50);

		series_ligne_1.add(new FixedMillisecond(today.getTime() + 800), 12.55);

		series_ligne_1.add(new FixedMillisecond(today.getTime() + 1000), 4.55);
*/


	// Activation de l'ajout dans le graphique

		//series_ligne_1.setNotify(true);



		/*series_ligne_2.setNotify(false);



		series_ligne_2.add(new Hour(1,5,2,2005), 1.55);

		series_ligne_2.add(new Hour(2,5,2,2005), 15.05);

		series_ligne_2.add(new Hour(3,5,2,2005), 16.55);

		series_ligne_2.add(new Hour(4,5,2,2005), 45.55);

		series_ligne_2.add(new Hour(5,5,2,2005), 35.55);

		series_ligne_2.add(new Hour(6,5,2,2005), 39.55);



		series_ligne_2.setNotify(true);*/



	// Titre du graphique

		chart.setTitle("Latence (ms)");



		chartPanel.setChart(chart);
        chart.setBackgroundPaint(new Color(200,200,200));



    }

    public void ajoutpoint(long x,long y){

        final long[] value = new long[2];
       Runnable updater = new Runnable() {
           public void run() {
               series_ligne_1.addOrUpdate(new FixedMillisecond( value[0]),  value[1]);
           }
       };

          //try {
              value[0] = x;
              value[1] = y;
             // SwingUtilities.inv(updater);
               series_ligne_1.addOrUpdate(new FixedMillisecond( value[0]),  value[1]);
          //}
          /*catch (InvocationTargetException e) {
              e.printStackTrace();
          }
          catch (InterruptedException e) {
              e.printStackTrace();
          }*/


	//series_ligne_1.addOrUpdate(new FixedMillisecond(x), y);
	// Activation de l'ajout dans le graphique

    series_ligne_1.setNotify(true);


}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(itrafgen.ItrafgenApp.class).getContext().getResourceMap(GraphBox.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 5, true));
        setMaximumSize(new java.awt.Dimension(700, 225));
        setMinimumSize(new java.awt.Dimension(120, 98));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(500, 225));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 490, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 215, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
